# Version 13.12.30

#欢迎界面
function welcome() {
echo ""
echo -e "========================= \033[32;49;1mWelcome to LNMP 2013 Final\033[39;49;0m ==========================="
echo ""
echo "       LNMP : Linux + Nginx + MySQL + PHP"
echo "    Version : 2013-Final (By Luxiaok.com)"
echo -e "     System : \033[32;49;1m${linux_release}\033[39;49;0m"
echo -e "   Platform : \033[32;49;1m$bit\033[39;49;0m"
echo ""
echo "================================================================================"
echo ""
}

welcome_lamp() {
echo ""
echo -e "========================= \033[32;49;1mWelcome to LAMP 2013 Final\033[39;49;0m ==========================="
echo ""
echo "       LAMP : Linux + Apache + MySQL + PHP"
echo "    Version : 2013-Final (By Luxiaok.com)"
echo -e "     System : \033[32;49;1m${linux_release}\033[39;49;0m"
echo -e "   Platform : \033[32;49;1m$bit\033[39;49;0m"
echo ""
echo "================================================================================"
echo ""
}

# 当前登录用户检测
check_current_user() {
if [ "`id -u`" != "0" ]; then
    echo ""
    logs "Error" "You must be root to run this script! "
    echo ""
    echo -e "Error: Please use \033[31;49;1mroot\033[39;49;0m to install LNMP! "
    echo ""
    exit 1
fi
}

# 系统检测
system_check() {
# 系统类型检测(RHEL/CentOS/Ubuntu)
if [ -f /etc/issue ] ; then
	linux_type=`cat /etc/issue | head -1 | awk '{print tolower($1)}'`
	if [ "$linux_type" = "centos" -o "$linux_type" = "red" ] ; then
		linux_type="rhel"
	elif [ "$linux_type" = "ubuntu" ] ; then
		linux_type="ubuntu"
	else
		linux_type="unknow"
		logs "Error" "Can not detect the system type."
		exit 1
  fi
else
	logs "Error" "Can not detect the system type."
	exit 1
fi
# 发行版检测
linux_release="`cat /etc/issue | head -1 | sed 's#\\\#:#g' | awk -F: '{print $1}'`"
# 系统平台检测(32位/64位)
if [ "`getconf LONG_BIT`" = "32" -a "`getconf WORD_BIT`" = "32" ] ; then
	linux_platform="32"
	bit="x86"
elif [ "`getconf LONG_BIT`" = "64" -a "`getconf WORD_BIT`" = "32" ] ; then
	linux_platform="64"
	bit="x64"
else
	linux_platform="0"
	bit="0"
	logs "Error" "Can not detect the system platform."
	exit 1
fi
}

# 时间校对
NTP_CN() {
rm -rf /etc/localtime
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
if [ ! -f /usr/sbin/ntpdate ] ; then
    if [ "$linux_type" = "ubuntu" ] ; then
        apt-get install ntpdate -y
    elif [ "$linux_type" = "rhel" ] ; then
        yum install -y ntpdate
    else
        echo "Error: Can not find the command \'ntpdate\'"
    fi
fi
ntpdate cn.pool.ntp.org
}

# 系统优化
Optimization() {
ulimit -v unlimited
cat >>/etc/security/limits.conf<<EOF
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
EOF
echo "fs.file-max = 65535" >> /etc/sysctl.conf
sed -i 's/# kernel.shmmax = 4294967295/kernel.shmmax = 128000000/g' /etc/sysctl.conf
sed -i 's/kernel.shmmax = 4294967295/kernel.shmmax = 128000000/g' /etc/sysctl.conf
ulimit -SHn 65535
sysctl -p
}

# 新建用户
user_add() {
cat /etc/group | awk -F: '{print $1}' | grep -x $1 >/dev/null
if [ "$?" != "0" ] ; then
   groupadd -r $1
else
   logs "Error" "The group \"$1\" already exist."
fi

id -u $1 >/dev/null 2>&1
if [ "$?" != "0" ] ; then
   if [ "$linux_type" = "rhel" ] ; then
      useradd -M -r -s /sbin/nologin -g $1 $1
   else
      useradd -M -r -s /usr/sbin/nologin -g $1 $1
   fi
else
   logs "Error" "The user \"$1\" already exist."
fi
}

# 按任意键继续
get_char()
{
	SAVEDSTTY=`stty -g`
	stty -echo
	stty cbreak
	dd if=/dev/tty bs=1 count=1 2> /dev/null
	stty -raw
	stty echo
	stty $SAVEDSTTY
}
